<?php

/**
 * Admin_Monitor_Hdd Model
 * 
 * Short file description
 *
 * PHP Version: 5.3.26
 * 
 * @category Admin_Monitor_Hdd_Models
 * @package  Admin_Monitor_Hdd_Package
* @author   Johari <joharijumali@gmail.com>
 */

namespace Admin\Models\Monitor;

use \Laravel\Database\Eloquent\Model as Eloquent;


/**
 * Class Admin_Monitor_Hdd Model
 * 
 * Class Description
 *
 * @category Admin_Monitor_Hdd_Models
 * @package  Admin_Monitor_Hdd_Package
 * @author   
 * @license  Laravel License
 * @link     
 */

class Hdd extends Eloquent 
{

	public static $timestamps = true;
    public static $table = 'server_hdd';
    public static $key = 'hdd_id';

	/**
     * Function cron that pass parameter
     *
     * @param integer $id   the integer to quote
     * @param varchar $data the varchar to quote
     * @param varchar $date the varchar to quote
	 *
	 * @return parameter.
     */
    public static function cron($id, $data, $date)
	{	
        $data = json_decode($data);

        $exist = Hdd::where('server_id', '=', $id)->first(array('hdd_id'));

			foreach ($data as $key => $value) {

				$Hdd = (empty($exist))? new Hdd : Hdd::find($exist->hdd_id);

		        $Hdd->server_id = $id;
		        $Hdd->hdd_name = $key;
		        $Hdd->hdd_free = $value->free;
		        $Hdd->hdd_total = $value->total;
		        $Hdd->hdd_rrqms = $value->rrqms;
		        $Hdd->hdd_wrqms = $value->wrqms;
		        $Hdd->hdd_rs = $value->rs;
		        $Hdd->hdd_ws = $value->ws;
		        $Hdd->hdd_rMBs = $value->rMBs;
		        $Hdd->hdd_wMBs = $value->wMBs;
		        $Hdd->hdd_avgrq = $value->avgrq;
		        $Hdd->hdd_avgqu = $value->avgqu;
		        $Hdd->hdd_await = $value->await;
		        $Hdd->hdd_svctm = $value->svctm;
		        $Hdd->hdd_util = $value->util;
		        $Hdd->hdd_date = $date;
		        $Hdd->save();

			}


        Hdd::_stor($id, $data, $date, 'stor_hdd');
    }
	
	/**
     * Function stor that pass parameter
     *
     * @param integer $id    the integer to quote
     * @param varchar $data  the varchar to quote
     * @param varchar $date  the varchar to quote
     * @param varchar $table the varchar to quote
	 *
	 * @return parameter.
     */
    protected static function _stor($id, $data, $date, $table)
	{
        static::$table = $table;

			foreach ($data as $key => $value) {

				$Hdd = new Hdd;

		        $Hdd->server_id = $id;
		        $Hdd->hdd_name = $key;
		        $Hdd->hdd_free = $value->free;
		        $Hdd->hdd_total = $value->total;
		        $Hdd->hdd_rrqms = $value->rrqms;
		        $Hdd->hdd_wrqms = $value->wrqms;
		        $Hdd->hdd_rs = $value->rs;
		        $Hdd->hdd_ws = $value->ws;
		        $Hdd->hdd_rMBs = $value->rMBs;
		        $Hdd->hdd_wMBs = $value->wMBs;
		        $Hdd->hdd_avgrq = $value->avgrq;
		        $Hdd->hdd_avgqu = $value->avgqu;
		        $Hdd->hdd_await = $value->await;
		        $Hdd->hdd_svctm = $value->svctm;
		        $Hdd->hdd_util = $value->util;
		        $Hdd->hdd_date = $date;
		        $Hdd->save();

			}
    }

    /**
     * Function grabSingleLoad that retrive parameter
     *
     * @param integer $server_id   tserver id
     *
     * @return parameter.
     */
    public static function grabSingleHdd($server_id)
    {
        
        $Hdd = Hdd::where('server_id', '=', $server_id)->get();

        $data = array();

        foreach ($Hdd as $key => $value) {
        	$data[$value->hdd_name] = $value->to_array();
        }

        return $data;
    }


    /**
     * Function grabSingleLoad that retrive parameter
     *
     * @param integer $server_id   tserver id
     *
     * @return parameter.
     */
    public static function grabAllHdd()
    {
        
        $Hdd = Hdd::all();

        $data = array();

        foreach ($Hdd as $key => $value) {
            $data[$value->server_id][$value->hdd_name] = $value->to_array();

            unset($data[$value->server_id]['created_at']);
            unset($data[$value->server_id]['updated_at']);
            unset($data[$value->server_id]['hdd_date']);
        }



        return $data;
    }

}
